# -*- coding:utf-8 -*-
# @author='dapeng',
# @version='1.0.0'
import requests,json
import urllib
import urllib.request
import configparser

cf = configparser.ConfigParser()
cf.read("D:/Program Files/pycharm/wechat-robot/configuration/config.ini")  # 读取配置文件，如果写文件的绝对路径，就可以不用os模块

address = cf.get("Email", "address")
password = cf.get("Email", "password")


#登录网易云
def login():
    data={}
    data['email']=address
    data['password']=password
    url_parame=urllib.parse.urlencode(data)
    url="https://autumnfish.cn/login?"
    all_url=url+url_parame
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    cookie = jsonstr.get('cookie')
    return cookie



#退出
def logout():
    requests.get('https://autumnfish.cn/logout')



#登录状态
def status():
    res = requests.get('https://autumnfish.cn/login/status')
    code = json.loads(res.text).get('code')
    print(code)



#找到音乐
def getmusic(mid):
    cookie = login()
    music = ''
    res = requests.get('https://autumnfish.cn//song/url?id=%s'%mid)
    jsonstr = json.loads(res.text)
    code = jsonstr.get('code')
    if code == 200:
        music = jsonstr.get('data')[0].get('url')
        logout()
        return music


#分享音乐，必须登录
def share(mid):
    cookie = login()
    data = {}
    data['id']=mid
    data['msg']='dapeng'
    data['cookie']=cookie
    url_param=urllib.parse.urlencode(data)
    url = "https://autumnfish.cn/share/resource?"
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    logout()
    return jsonstr.get('resUrl')


#查找音乐
def search(text):
    cookie = login()
    data = {}
    data['keywords']=text
    data['cookie']=cookie
    url_param=urllib.parse.urlencode(data)
    url = "https://autumnfish.cn/search?"
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    id = jsonstr.get('result').get('songs')[0].get('id')
    imgurl = jsonstr.get('result').get('songs')[0].get('artists')[0].get('img1v1Url')
    logout()
    return id,imgurl
